<?php
namespace app\auth\controller;
use think\Controller;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
class Base extends Controller{
    
//七牛上传调用插件
    public function qiniu($file,$folder='',$is_https=null){
        $qiniu_config = $is_https?config('qiniu_https'):config('qiniu');
        require_once VENDOR_PATH.'/qiniu/php-sdk/autoload.php';
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = $qiniu_config['ACCESSKEY'];
        $secretKey = $qiniu_config['SECRETKEY'];
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket =  $qiniu_config['BUCKET'];

        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        // 要上传文件的本地路径
        //$filePath = $file->getRealPath();
        $filePath = $file;

        //$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        // 上传到七牛后保存的文件名
        $key = $folder.substr(md5($filePath) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return array('status'=>0,'msg'=>$err,'data'=>'');
        } else {
            //$url = $qiniu_config['DOMAIN'].'/'.$key;
            $url = '/'.$key;
            return array('status'=>1,'msg'=>'上传成功','data'=>$url);
        }
    }


    public function qiniu_put($data,$folder='',$config_name='qiniu_https'){
        $qiniu_config = config($config_name);
        $folder =  !empty($folder)?$folder:'live/miniprogram/qrcode';
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = $qiniu_config['ACCESSKEY'];
        $secretKey = $qiniu_config['SECRETKEY'];
        // 构建鉴权对象
        require_once VENDOR_PATH.'/qiniu/php-sdk/autoload.php';
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket =  $qiniu_config['BUCKET'];
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        $key = $folder.substr(md5('smallMaps') , 0, 5). date('YmdHis') . rand(0, 9999) . '.png';
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
//        echo $key;die;

        list($ret, $err) = $uploadMgr->put($token, $key, $data);
        if ($err !== null) {
            $result=array('status'=>0,'msg'=>$err,'data'=>'');
        } else {
            $result=(array('status'=>1,'msg'=>'上传成功','data'=>'/'.$key));
        }

        if($result['status']==1){
            //unlink($newFilePath);
            $return=[
                'data'=>$qiniu_config['DOMAIN'].$result['data'],
                'org_data'=>$result['data'],
                'status'=>1,
                'msg'=>'sucess'
            ];
        }else{
            $return=[
                'msg'=>'fail',
                'status'=>0,
                'data'=>[]
            ];
        }
        return $return;
    }


    //上传--调用方法
    /**
     * @param array $config 文件大小与类型的配置
     * @param string $folder 文件夹
     * @return mixed
     */
    public function uploadfile_qiniu($config=[],$folder=''){
        $folder = $folder?$folder.'/':'yaliapi/';
        $config = array(
            'size' =>   $config['size']??1048576,//默认1M
            'ext'  =>   $config['ext']??'png,jpg,jpeg,gif,bmp',
        );
        foreach($_FILES as $key=>$val){
            $file = request()->file($key);
            //区分单文件与多文件
            if(is_array($file)){
                //先验证
                foreach($file as $k =>$v){
                    $v->validate($config)->check();
                    $error_msg = $v->getError();
                    //再上传
                    if($error_msg){
                        return show_result(0,$error_msg);
                    }else{
                        $file_qiniu = $this->qiniu($v,$folder);
                        if($file_qiniu['status']==1){
                            $file_url['url'] = $file_qiniu['data'];
                            $file_info[$k] = array_merge($v->getInfo(),$file_url);
                        }else{
                            return show_result(0,$file_qiniu['msg']);
                        }
                    }
                }
                return show_result(1,'上传成功',$file_info);
            }else{
                $file->validate($config)->check();
                $error_msg = $file->getError();
                //再上传
                if($error_msg){
                    return show_result(0,$error_msg);
                }else{
                    $file_qiniu = $this->qiniu($file,$folder);
                    if($file_qiniu['status']==1){
                        $file_url['url'] = $file_qiniu['data'];
                        return show_result(1,'上传成功',array_merge($val,$file_url));
                    }else{
                        return show_result(0,$file_qiniu['msg']);
                    }
                }
            }
        }
    }
}